<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="交易记录号" prop="tradeId">
        <el-input
          v-model="queryParams.tradeId"
          placeholder="请输入交易记录号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款人ID" prop="custId">
        <el-input
          v-model="queryParams.custId"
          placeholder="请输入付款人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款人帐号" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入付款人帐号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款人" prop="custName">
        <el-input
          v-model="queryParams.custName"
          placeholder="请输入付款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易单号" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入交易单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易商品" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入交易商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分佣会员ID" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入分佣会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分佣会员帐号" prop="accountNo">
        <el-input
          v-model="queryParams.accountNo"
          placeholder="请输入分佣会员帐号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分佣会员姓名" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入分佣会员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算源" prop="calcField">
        <el-input
          v-model="queryParams.calcField"
          placeholder="请输入计算源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算值" prop="calcValue">
        <el-input
          v-model="queryParams.calcValue"
          placeholder="请输入计算值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分配方案" prop="scheme">
        <el-input
          v-model="queryParams.scheme"
          placeholder="请输入分配方案"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收入科目" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入收入科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可消费余额" prop="custBlance">
        <el-input
          v-model="queryParams.custBlance"
          placeholder="请输入可消费余额 只能用于消费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现金 可提现" prop="withdrawable">
        <el-input
          v-model="queryParams.withdrawable"
          placeholder="请输入现金 可提现"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分" prop="custPoint">
        <el-input
          v-model="queryParams.custPoint"
          placeholder="请输入积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算日期" prop="clcDate">
        <el-input
          v-model="queryParams.clcDate"
          placeholder="请输入计算日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入状态 【 1 交易中 2 已完成 3 撤销，4 可提现】依据交易记录生成"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.state"
          clearable
          placeholder="请选择"
          @keyup.enter.native="handleQuery"
          style="width: 215px"
        >
          <el-option
            v-for="dict in dict.type.sys_trade_state"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业帐号" prop="sysAccount">
        <el-input
          v-model="queryParams.sysAccount"
          placeholder="请输入企业帐号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提成比例" prop="deductScale">
        <el-input
          v-model="queryParams.deductScale"
          placeholder="请输入提成比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下级截取比例" prop="custScale">
        <el-input
          v-model="queryParams.custScale"
          placeholder="请输入下级截取比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="升级分数" prop="updateValue">
        <el-input
          v-model="queryParams.updateValue"
          placeholder="请输入升级分数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['web:allot:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['web:allot:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:allot:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['web:allot:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="allotList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="主键id"
        align="center"
        prop="id"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="交易记录号"
        align="center"
        prop="tradeId"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="付款人ID"
        align="center"
        prop="custId"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="付款人帐号"
        align="center"
        prop="custNo"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="付款人"
        align="center"
        prop="custName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="交易单号"
        align="center"
        prop="tradeNo"
        min-width="120"
        show-overflow-tooltip
      />
      <!-- <el-table-column label="交易类别 【1 商品购买 2 充值 3.旅行体验券，4.拓店】" align="center" prop="tradeType" /> -->
      <el-table-column label="交易类别" align="center" prop="tradeType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_trade_type"
            :value="scope.row.tradeType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="交易商品"
        align="center"
        prop="productName"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="分佣会员ID"
        align="center"
        prop="accountId"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="分佣会员帐号"
        align="center"
        prop="accountNo"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="分佣会员姓名"
        align="center"
        prop="accountName"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="计算源"
        align="center"
        prop="calcField"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="计算值"
        align="center"
        prop="calcValue"
        min-width="100"
      />
      <el-table-column
        label="分配方案"
        align="center"
        prop="scheme"
        min-width="100"
      />
      <el-table-column
        label="收入科目"
        align="center"
        prop="subject"
        min-width="100"
      />
      <el-table-column
        label="可消费余额"
        align="center"
        prop="custBlance"
        min-width="120"
      />
      <el-table-column
        label="现金 可提现"
        align="center"
        prop="withdrawable"
        min-width="120"
      />
      <el-table-column
        label="积分"
        align="center"
        prop="custPoint"
        min-width="100"
      />
      <el-table-column
        label="计算日期"
        align="center"
        prop="clcDate"
        min-width="160"
      />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_trade_state"
            :value="scope.row.state"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="企业帐号"
        align="center"
        prop="sysAccount"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="提成比例"
        align="center"
        prop="deductScale"
        min-width="100"
      />
      <el-table-column
        label="下级截取比例"
        align="center"
        prop="custScale"
        min-width="100"
      />
      <el-table-column
        label="升级分数"
        align="center"
        prop="updateValue"
        min-width="100"
      />
      <el-table-column
        label="解冻货款状态"
        align="center"
        prop="blanceType"
        min-width="120"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.blance_type"
            :value="scope.row.blanceType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="佣金类型"
        align="center"
        prop="commiType"
        min-width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.commi_type"
            :value="scope.row.commiType"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="" align="center" prop="version" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['web:allot:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['web:allot:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分佣明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="交易记录号" prop="tradeId">
          <el-input v-model="form.tradeId" placeholder="请输入交易记录号" />
        </el-form-item>
        <el-form-item label="付款人ID" prop="custId">
          <el-input v-model="form.custId" placeholder="请输入付款人ID" />
        </el-form-item>
        <el-form-item label="付款人帐号" prop="custNo">
          <el-input v-model="form.custNo" placeholder="请输入付款人帐号" />
        </el-form-item>
        <el-form-item label="付款人" prop="custName">
          <el-input v-model="form.custName" placeholder="请输入付款人" />
        </el-form-item>
        <el-form-item label="交易单号" prop="tradeNo">
          <el-input v-model="form.tradeNo" placeholder="请输入交易单号" />
        </el-form-item>
        <el-form-item label="交易商品" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入交易商品" />
        </el-form-item>
        <el-form-item label="分佣会员ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入分佣会员ID" />
        </el-form-item>
        <el-form-item label="分佣会员帐号" prop="accountNo">
          <el-input v-model="form.accountNo" placeholder="请输入分佣会员帐号" />
        </el-form-item>
        <el-form-item label="分佣会员姓名" prop="accountName">
          <el-input
            v-model="form.accountName"
            placeholder="请输入分佣会员姓名"
          />
        </el-form-item>
        <el-form-item label="计算源" prop="calcField">
          <el-input v-model="form.calcField" placeholder="请输入计算源" />
        </el-form-item>
        <el-form-item label="计算值" prop="calcValue">
          <el-input v-model="form.calcValue" placeholder="请输入计算值" />
        </el-form-item>
        <el-form-item label="分配方案" prop="scheme">
          <el-input v-model="form.scheme" placeholder="请输入分配方案" />
        </el-form-item>
        <el-form-item label="收入科目" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入收入科目" />
        </el-form-item>
        <el-form-item label="可消费余额" prop="custBlance">
          <el-input
            v-model="form.custBlance"
            placeholder="请输入可消费余额 只能用于消费"
          />
        </el-form-item>
        <el-form-item label="现金 可提现" prop="withdrawable">
          <el-input
            v-model="form.withdrawable"
            placeholder="请输入现金 可提现"
          />
        </el-form-item>
        <el-form-item label="积分" prop="custPoint">
          <el-input v-model="form.custPoint" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="计算日期" prop="clcDate">
          <el-input v-model="form.clcDate" placeholder="请输入计算日期" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <!-- <el-input v-model="form.state" placeholder="请输入状态 【 1 交易中 2 已完成 3 撤销，4 可提现】依据交易记录生成" /> -->
          <el-select
            v-model="form.state"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_trade_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业帐号" prop="sysAccount">
          <el-input v-model="form.sysAccount" placeholder="请输入企业帐号" />
        </el-form-item>
        <el-form-item label="提成比例" prop="deductScale">
          <el-input v-model="form.deductScale" placeholder="请输入提成比例" />
        </el-form-item>
        <el-form-item label="下级截取比例" prop="custScale">
          <el-input v-model="form.custScale" placeholder="请输入下级截取比例" />
        </el-form-item>
        <el-form-item label="升级分数" prop="updateValue">
          <el-input v-model="form.updateValue" placeholder="请输入升级分数" />
        </el-form-item>
        <!-- <el-form-item label="" prop="version">
          <el-input v-model="form.version" placeholder="请输入" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAllot,
  getAllot,
  delAllot,
  addAllot,
  updateAllot,
} from "@/api/djk/journalAccount/subcommission";

export default {
  name: "Allot",
  dicts: ["sys_trade_state", "sys_trade_type", "blance_type", "commi_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分佣明细表格数据
      allotList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tradeId: null,
        custId: null,
        custNo: null,
        custName: null,
        tradeNo: null,
        tradeType: null,
        productName: null,
        accountId: null,
        accountNo: null,
        accountName: null,
        calcField: null,
        calcValue: null,
        scheme: null,
        subject: null,
        custBlance: null,
        withdrawable: null,
        custPoint: null,
        clcDate: null,
        state: null,
        sysAccount: null,
        deductScale: null,
        custScale: null,
        updateValue: null,
        blanceType: null,
        commiType: null,
        version: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tradeId: [
          { required: true, message: "交易记录号不能为空", trigger: "blur" },
        ],
        custId: [
          { required: true, message: "付款人ID不能为空", trigger: "blur" },
        ],
        custNo: [
          { required: true, message: "付款人帐号不能为空", trigger: "blur" },
        ],
        tradeType: [
          { required: true, message: "交易类别 不能为空", trigger: "change" },
        ],
        custBlance: [
          {
            required: true,
            message: "可消费余额 只能用于消费不能为空",
            trigger: "blur",
          },
        ],
        withdrawable: [
          { required: true, message: "现金 可提现不能为空", trigger: "blur" },
        ],
        custPoint: [
          { required: true, message: "积分不能为空", trigger: "blur" },
        ],
        deductScale: [
          { required: true, message: "提成比例不能为空", trigger: "blur" },
        ],
        custScale: [
          { required: true, message: "下级截取比例不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询分佣明细列表 */
    getList() {
      this.loading = true;
      listAllot(this.queryParams).then((response) => {
        this.allotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tradeId: null,
        custId: null,
        custNo: null,
        custName: null,
        tradeNo: null,
        tradeType: null,
        productName: null,
        accountId: null,
        accountNo: null,
        accountName: null,
        calcField: null,
        calcValue: null,
        scheme: null,
        subject: null,
        custBlance: null,
        withdrawable: null,
        custPoint: null,
        clcDate: null,
        state: null,
        sysAccount: null,
        deductScale: null,
        custScale: null,
        updateValue: null,
        blanceType: null,
        commiType: null,
        version: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分佣明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAllot(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分佣明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAllot(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAllot(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除分佣明细编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAllot(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "web/allot/export",
        {
          ...this.queryParams,
        },
        `allot_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
